<?php

namespace App\Models\ReportForms;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class PriceForms extends Model
{
    protected $table = 'price_forms';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList($request, $memberinfo, $isgetall)
    {
        $start_time = $request->start_time ? strtotime($request->start_time) : time() - 86400 * 30;
        $end_time = $request->end_time ? strtotime($request->end_time) : time();
        $is_all = $request->is_all ?? 1;

        $query = self::select(DB::raw('date,SUM(purchase_price) as purchase_price,SUM(sale_price) as sale_price,SUM(website_price) as website_price,SUM(operate_price) as operate_price,SUM(future_price) as future_price,SUM(diff_price) as diff_price'))            
            ->groupBy('date', 'create_at')->orderBy('create_at', 'asc');

        if ($start_time > 0 && $end_time > 0) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        if ($is_all == 2) {
            $cpid = $memberinfo['cpid'] ?? 0;
            if ($cpid > 0) {
                $query->where('cpid', $cpid);
            }
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
}